/*
 * ApoioView.java
 */
package asfabdesk.apresentacao;

import asfabdesk.dominio.Usuario;
import asfabdesk.presistencia.DAO;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.border.TitledBorder;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.TitlePaneLayout;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;

/**
 * The application's main frame.
 */
public class TelaLogin extends FrameView {

    public static TelaPrincipal telaPrincipal;
    public static Usuario usuario;
    private static Properties props = new Properties();
    public static String ip;
    public static String porta;
    public static String caminho_img;
    public static String banco;

    public TelaLogin(SingleFrameApplication app) throws IOException {
        super(app);
        initComponents();
        config();        
        this.getFrame().setTitle("ASFAB_desk");
    }

     public static void config() throws IOException {
        FileInputStream entrada = new FileInputStream("config.properties");
        props.load(entrada);
        entrada.close();
        Properties applicationProps = new Properties(props);
        ip = applicationProps.getProperty("ip_servidor");
        porta = applicationProps.getProperty("porta");
        caminho_img = applicationProps.getProperty("caminho_img");
        banco=applicationProps.getProperty("banco");
    }

    public void Logar() {
        if (verificarCampos()) {
            if (validarLogin()) {
                telaPrincipal = new TelaPrincipal();
                telaPrincipal.setVisible(true);
                telaPrincipal.setExtendedState(2 | 4);
                getFrame().dispose();

            } else {
                JOptionPane.showMessageDialog(null, "Usuário/Senha Incorretos: verifique o Caps Lock e tente novamente.", "Usuário/Senha Incorretos", 0);
                txtLogin.setText("");
                txtSenha.setText("");

            }
        }
    }

    public boolean validarLogin() {
        usuario = new DAO().localizar(Usuario.class, "UsuarioPorLogin", txtLogin.getText());
        if (usuario != null) {
            if (usuario.getSenha().equals(txtSenha.getText())) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public boolean verificarCampos() {
        if (txtLogin.getText().equals("") || txtSenha.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Ops, preencha corretamente os campos Usuário/Senha.", "Ops", 1);
            return false;
        } else {
            return true;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtSenha = new javax.swing.JPasswordField();
        txtLogin = new javax.swing.JTextField();
        btnLogar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(asfabdesk.apresentacao.AsfabDeskApp.class).getContext().getResourceMap(TelaLogin.class);
        mainPanel.setToolTipText(resourceMap.getString("mainPanel.toolTipText")); // NOI18N
        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jPanel2.setBackground(resourceMap.getColor("jPanel2.background")); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setForeground(resourceMap.getColor("jLabel5.foreground")); // NOI18N
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N
        jPanel2.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 100, 110, -1));

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setForeground(resourceMap.getColor("jLabel4.foreground")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N
        jPanel2.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 70, -1, -1));

        txtSenha.setForeground(resourceMap.getColor("txtSenha.foreground")); // NOI18N
        txtSenha.setText(resourceMap.getString("txtSenha.text")); // NOI18N
        txtSenha.setName("txtSenha"); // NOI18N
        txtSenha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSenhaActionPerformed(evt);
            }
        });
        jPanel2.add(txtSenha, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 100, 130, -1));

        txtLogin.setFont(resourceMap.getFont("txtLogin.font")); // NOI18N
        txtLogin.setForeground(resourceMap.getColor("txtLogin.foreground")); // NOI18N
        txtLogin.setText(resourceMap.getString("txtLogin.text")); // NOI18N
        txtLogin.setName("txtLogin"); // NOI18N
        jPanel2.add(txtLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 70, 200, 20));

        btnLogar.setText(resourceMap.getString("btnLogar.text")); // NOI18N
        btnLogar.setName("btnLogar"); // NOI18N
        btnLogar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLogarActionPerformed(evt);
            }
        });
        jPanel2.add(btnLogar, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 100, 60, -1));

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setIcon(resourceMap.getIcon("jLabel3.icon")); // NOI18N
        jLabel3.setToolTipText(resourceMap.getString("jLabel3.toolTipText")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N
        jPanel2.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 490, 280));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        mainPanel.add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 490, 290));

        setComponent(mainPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void btnLogarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLogarActionPerformed
        Logar();
    }//GEN-LAST:event_btnLogarActionPerformed

    private void txtSenhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSenhaActionPerformed
        Logar();
    }//GEN-LAST:event_txtSenhaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLogar;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JTextField txtLogin;
    private javax.swing.JPasswordField txtSenha;
    // End of variables declaration//GEN-END:variables
}
